package com.ruoyi.basebill.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.basebill.domain.OmsSaleorderbillentry;
import com.ruoyi.basebill.mapper.OmsSaleorderbillMapper;
import com.ruoyi.basebill.domain.OmsSaleorderbill;
import com.ruoyi.basebill.service.IOmsSaleorderbillService;

/**
 * OMS销售订单Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-02-08
 */
@Service
public class OmsSaleorderbillServiceImpl implements IOmsSaleorderbillService 
{
    @Autowired
    private OmsSaleorderbillMapper omsSaleorderbillMapper;

    @Autowired
    private OmsSaleorderbillServiceImpl saleorderbillService;

    /**
     * 查询OMS销售订单
     * 
     * @param id OMS销售订单主键
     * @return OMS销售订单
     */
    @Override
    public OmsSaleorderbill selectOmsSaleorderbillById(Long id)
    {
        return omsSaleorderbillMapper.selectOmsSaleorderbillById(id);
    }

    /**
     * 查询OMS销售订单列表
     * 
     * @param omsSaleorderbill OMS销售订单
     * @return OMS销售订单
     */
    @Override
    public List<OmsSaleorderbill> selectOmsSaleorderbillList(OmsSaleorderbill omsSaleorderbill)
    {
        return omsSaleorderbillMapper.selectOmsSaleorderbillList(omsSaleorderbill);
    }

    @Override
    public List<OmsSaleorderbill> selectOmsSaleorderbillDetailList(OmsSaleorderbill omsSaleorderbill) {
        return omsSaleorderbillMapper.selectOmsSaleorderbillDetailList(omsSaleorderbill);
    }

    /**
     * 新增OMS销售订单
     * 
     * @param omsSaleorderbill OMS销售订单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertOmsSaleorderbill(OmsSaleorderbill omsSaleorderbill)
    {
        int rows = omsSaleorderbillMapper.insertOmsSaleorderbill(omsSaleorderbill);
        saleorderbillService.insertOmsSaleorderbillentry(omsSaleorderbill);
        return rows;
    }

    /**
     * 修改OMS销售订单
     * 
     * @param omsSaleorderbill OMS销售订单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOmsSaleorderbill(OmsSaleorderbill omsSaleorderbill)
    {
        if (omsSaleorderbill.getOmsSaleorderbillentryList() != null && omsSaleorderbill.getOmsSaleorderbillentryList().size() != 0){
            omsSaleorderbillMapper.deleteOmsSaleorderbillentryByParentid(omsSaleorderbill.getId());
            saleorderbillService.insertOmsSaleorderbillentry(omsSaleorderbill);
        }
        return omsSaleorderbillMapper.updateOmsSaleorderbill(omsSaleorderbill);
    }

    /**
     * 批量删除OMS销售订单
     * 
     * @param ids 需要删除的OMS销售订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOmsSaleorderbillByIds(Long[] ids)
    {
        omsSaleorderbillMapper.deleteOmsSaleorderbillentryByParentids(ids);
        return omsSaleorderbillMapper.deleteOmsSaleorderbillByIds(ids);
    }

    /**
     * 删除OMS销售订单信息
     * 
     * @param id OMS销售订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOmsSaleorderbillById(Long id)
    {
        omsSaleorderbillMapper.deleteOmsSaleorderbillentryByParentid(id);
        return omsSaleorderbillMapper.deleteOmsSaleorderbillById(id);
    }

    /**
     * 新增OMS销售订单明细信息
     * 
     * @param omsSaleorderbill OMS销售订单对象
     */
    public void insertOmsSaleorderbillentry(OmsSaleorderbill omsSaleorderbill)
    {
        List<OmsSaleorderbillentry> omsSaleorderbillentryList = omsSaleorderbill.getOmsSaleorderbillentryList();
        Long id = omsSaleorderbill.getId();
        if (StringUtils.isNotNull(omsSaleorderbillentryList))
        {
            List<OmsSaleorderbillentry> list = new ArrayList<OmsSaleorderbillentry>();
            for (OmsSaleorderbillentry omsSaleorderbillentry : omsSaleorderbillentryList)
            {
                omsSaleorderbillentry.setParentid(id);
                list.add(omsSaleorderbillentry);
            }
            if (list.size() > 0)
            {
                omsSaleorderbillMapper.batchOmsSaleorderbillentry(list);
            }
        }
    }
}
